package com.baselibrary.utils;


import android.annotation.SuppressLint;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class TimeUtil {

    public static String getPartyTime(long time) {
        return new SimpleDateFormat("MM月dd日", Locale.getDefault()).format(new Date(time)) + " " + CommonUtil.getWeek(time) + " " + getTwoTime(time);
    }

    public static String getOneTime(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
        return formatter.format(date);
    }

    public static String getMMdd(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("MM.dd", Locale.getDefault());
        return formatter.format(date);
    }

    public static String getMM_dd(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd", Locale.getDefault());
        return formatter.format(date);
    }

    public static String getMMdd_week(long time) {
        Date date = new Date(time);
        String StringWeek = getWeekOfDate(date);
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 ", Locale.getDefault());
        return formatter.format(date) + StringWeek;
    }

    public static String getTwoTime(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return formatter.format(date);
    }

    public static String getThreeTime(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return formatter.format(date);
    }

    public static String getTimew(long time) {
        if (time == 0L) return "";
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        return formatter.format(date);
    }

    public static String getTimeT(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault());
        return formatter.format(date);
    }

    /**
     * 订单相关时间格式化---年月日时分秒
     *
     * @author: merbng
     * @time: 2021/10/13 17:05
     * @description:
     */
    public static String setYearMonthDay(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return formatter.format(date);
    }

    public static String setYearMonthDay3(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return formatter.format(date);
    }

    /**
     * 获取七天后的日期
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getSevenDayTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 604800);
        String shur_time = dateFormat.format(calendar.getTime());
        return shur_time;
    }

    public static long getStringToDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getLongDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getLongDateTwo(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 时间格式化
     *
     * @param timeStamp 时间
     * @return 数据
     */
    public static String format(long timeStamp) {
        long curTimeMillis = System.currentTimeMillis();
        Date curDate = new Date(curTimeMillis);
        int todayHoursSeconds = curDate.getHours() * 60 * 60;
        int todayMinutesSeconds = curDate.getMinutes() * 60;
        int todaySeconds = curDate.getSeconds();
        int todayMillis = (todayHoursSeconds + todayMinutesSeconds + todaySeconds) * 1000;
        long todayStartMillis = curTimeMillis - todayMillis;
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdfone = new SimpleDateFormat("HH:mm");
        if (timeStamp >= todayStartMillis) {
            return "今天 " + sdfone.format(new Date(timeStamp));
        }
        int oneDayMillis = 24 * 60 * 60 * 1000;
        long yesterdayStartMilis = todayStartMillis - oneDayMillis;
        if (timeStamp >= yesterdayStartMilis) {
            return "昨天 " + sdfone.format(new Date(timeStamp));
        }
        long yesterdayBeforeStartMilis = yesterdayStartMilis - oneDayMillis;
        if (timeStamp >= yesterdayBeforeStartMilis) {
            return "前天 " + sdfone.format(new Date(timeStamp));
        }
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(new Date(timeStamp));
    }

    public static String formatMS(int duration) {
        int h = duration / 3600;
        int m = (duration - h * 3600) / 60;
        String min;
        String second;
        if (m < 10) {
            min = "0" + m;
        } else {
            min = m + "";
        }
        int s = (duration - h * 3600) % 60;
        if (s < 10) {
            second = "0" + s;
        } else {
            second = s + "";
        }
        return min + ":" + second;
    }


    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月


    /**
     * 获取当前日期是星期几
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /*获取当前时间的时间戳*/
    public static Long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    public static String getLiveTime(long timesamp) {
        String result = "";
        Calendar todayCalendar = Calendar.getInstance();
        Calendar otherCalendar = Calendar.getInstance();
        otherCalendar.setTimeInMillis(timesamp);
        String timeFormat = "yyyy-MM-dd HH:mm";
        int temp = otherCalendar.get(Calendar.DATE) - todayCalendar.get(Calendar.DATE);
        if (temp < 3 && temp >= 0) {
            switch (temp) {
                case 0:
                    result = "今天 " + getHourAndMin(timesamp);
                    break;
                case 1:
                    result = "明天 " + getHourAndMin(timesamp);
                    break;
                case 2:
                    result = "后天 " + getHourAndMin(timesamp);
                    break;
                default:
                    break;
            }
        } else {
            result = getTime(timesamp, timeFormat);
        }
        return result;
    }

    public static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }

    /**
     * 不同一周的显示时间格式
     *
     * @param time
     * @param timeFormat
     * @return
     */
    public static String getTime(long time, String timeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        return format.format(new Date(time));
    }


    public static String getTime(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return formatter.format(date);
    }
    public static String getMonthDayTime(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 HH:mm:ss", Locale.getDefault());
        return formatter.format(date);
    }
    public static String getYearMonthDay(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
        return formatter.format(date);
    }


    public static String setYearMonthDay2(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
        return formatter.format(date);
    }

    /**
     * 今天/昨天/前天/3天前…
     */
    public static String getDayAround(long time) {

        Date date = new Date(time);

        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > day) {
            //大于一天
            r = (diff / day);
            if (r == 1) {
                return "昨天";
            }
            if (r == 2) {
                return "前天";
            }
            if (r <= 9) {
                return r + "天前";
            }
            return "";
        } else {
            //小于于一天
            return "今天";
        }
    }

    public static String getRuleTime(long time, String format) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        return formatter.format(date);
    }

    /**
     * 长整型转化为可视的时间类型的字符串
     *
     * @param numData 长整型数字
     * @return 可读性时间
     */
    public static Date long2Data(long numData) {
        return new Date(numData);
    }


    public static boolean isCurrentWeek(long time) {
        int unknownWeek = getWeekAndDay(time);
        int currentWeek = getWeekAndDay(System.currentTimeMillis());
        return unknownWeek == currentWeek;
    }

    /**
     * 第几周
     *
     * @param time
     * @return
     */
    public static int getWeekAndDay(long time) {
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取当前时间为本月的第几周
        int week = calendar.get(Calendar.WEEK_OF_MONTH);
        //获取当前时间为本周的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == 1) {
            day = 7;
            week = week - 1;
        } else {
            day = day - 1;
        }
        Log.d("=====", "getWeekAndDay: " + week);
        return week;
    }

    //判断选择的日期是否是今天
    public static boolean isToday(long time) {
        return isThisTime(time, "yyyy-MM-dd");
    }

    private static boolean isThisTime(long time, String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    //判断选择的日期是否是本年
    public static boolean isThisYear(Long time) {
        return isThisTime(time, "yyyy");
    }

    /**
     * 距离今天多久
     *
     * @param date
     * @return
     */
    private static final long ONE_MINUTE = 60;
    private static final long ONE_HOUR = 3600;
    private static final long ONE_DAY = 86400;
    private static final long ONE_WEEK = 604800;
    private static final long ONE_YEAR = 31104000;

    public static String fromToday(Long numData) {
        Date date = long2Data(numData);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        long time = date.getTime() / 1000;
        long now = new Date().getTime() / 1000;
        long ago = now - time;

        if (ago <= ONE_HOUR)
            return ago / ONE_MINUTE == 0 ? "刚刚" : ago / ONE_MINUTE + "分钟前";
        else if (isToday(numData)) {
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
            return formatter.format(date);
        } else if (differFrom(numData, System.currentTimeMillis()) <= ONE_DAY) {
            return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + (calendar.get(Calendar.MINUTE) >= 10 ? calendar.get(Calendar.MINUTE) : "0" + calendar.get(Calendar.MINUTE)) + "";
        } else if (differFrom(numData, System.currentTimeMillis()) <= ONE_DAY * 2)
            return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + (calendar.get(Calendar.MINUTE) >= 10 ? calendar.get(Calendar.MINUTE) : "0" + calendar.get(Calendar.MINUTE)) + "";
        else if (isCurrentWeek(numData)) {//星期
            @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("EEEE");
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
            return format.format(date) + " " + formatter.format(date);
        } else if (isThisYear(numData)) {//本年
            SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
            return formatter.format(date);
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            return formatter.format(date);
        }
    }


    public static long differFrom(long appointTime, long currentTime) {
        if (currentTime > appointTime) {
            return getTodayTime(currentTime) / 1000 - getTodayTime(appointTime) / 1000;
        }
        return 0;
    }

    /**
     * 获取每日0点时间
     *
     * @return
     */
    public static Long getTodayTime(Long timeStamp) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeStamp);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

}

